package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.share.NotShareGood;
import cn.kooki.app.duobao.data.Bean.share.ShareListItem;
import cn.kooki.app.duobao.ui.Activity.Share.MyShareActivity;
import cn.kooki.app.duobao.util.ScreenUtils;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.TimeUtils;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xiong on 2015/12/17.
 */
public class ShareAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ShareListItem> datas;

    public ShareAdapter(Context context, ArrayList<ShareListItem> datas) {
        this.context = context;
        this.datas = datas;
    }



    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ShareListItem shareListItem = datas.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_share_item_new, parent, false);
            viewHolder = new ViewHolder(convertView);
            for (int i = 0; i < 3; i++) {
                ImageView imageView = new ImageView(context);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        ScreenUtils.dpToPxInt(context, 70), ScreenUtils.dpToPxInt(context, 70));
                imageView.setTag(3);
                layoutParams.rightMargin = (ScreenUtils.dpToPxInt(context, 16));
                imageView.setLayoutParams(layoutParams);
                imageView.setVisibility(View.INVISIBLE);
                viewHolder.shareListImages.addView(imageView);
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        Glide.with(context).load(shareListItem.getThumbs()).into(viewHolder.ivAvatar);
        viewHolder.shareListName.setText(shareListItem.username);
        viewHolder.shareListTitle.setText(shareListItem.title);
        viewHolder.goodsPeriod.setText("期号:" + shareListItem.qishu);
        viewHolder.shareListGoodName.setText(shareListItem.shopname1);
        viewHolder.shareListText.setText(shareListItem.content);
        viewHolder.shareListTime.setText(TimeUtils.getMouthTime(shareListItem.time));
        for (int i = 0; i < viewHolder.shareListImages.getChildCount(); i++) {
            ImageView imageView = (ImageView) viewHolder.shareListImages.getChildAt(i);
            if (i < shareListItem.photolist.size()) {
                Glide.with(context).load(shareListItem.photolist.get(i)).centerCrop().placeholder(R.drawable.img_blank).into(imageView);
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.INVISIBLE);
            }
        }
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_share_item_new.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.iv_avatar)
        CircleImageView ivAvatar;
        @Bind(R.id.share_list_time)
        TextView shareListTime;
        @Bind(R.id.share_list_name)
        TextView shareListName;
        @Bind(R.id.share_list_status)
        TextView shareListStatus;
        @Bind(R.id.share_list_title)
        TextView shareListTitle;
        @Bind(R.id.share_list_goodName)
        TextView shareListGoodName;
        @Bind(R.id.goods_period)
        TextView goodsPeriod;
        @Bind(R.id.share_list_text)
        TextView shareListText;
        @Bind(R.id.share_list_images)
        LinearLayout shareListImages;
        @Bind(R.id.share_list_content)
        LinearLayout shareListContent;
        @Bind(R.id.share_list_wrapper)
        RelativeLayout shareListWrapper;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
